﻿#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <stdint.h>
#include <list>
using namespace std;
// TODO:  在此处引用程序需要的其他头文件
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_XClient/XClient_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_XClient/XClient_Error.h"

#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/StreamMedia_XClient.lib")

FILE* pSt_VFile;
FILE* pSt_AFile;
LPCTSTR lpszVFile = _T("E:\\h264 file\\480p.264");
LPCTSTR lpszAFile = _T("E:\\audio\\1.aac");

int fread_video(LPVOID lParam, uint8_t* puszMsgBuffer, int nSize)
{
	printf("%d\n", nSize);
	int nRet = fread(puszMsgBuffer, 1, 1024, pSt_VFile);
	if (nRet <= 0)
	{
		fclose(pSt_VFile);
		pSt_VFile = _tfopen(lpszVFile, "rb");
		nRet = fread(puszMsgBuffer, 1, nSize, pSt_VFile);
		return nRet;
	}
	return nRet;
}
int fread_audio(LPVOID lParam, uint8_t* puszMsgBuffer, int nSize)
{
	
	printf("%d\n", nSize);
	int nRet = fread(puszMsgBuffer, 1, 1024, pSt_AFile);
	if (nRet <= 0)
	{
		fclose(pSt_AFile);
		pSt_AFile = _tfopen(lpszAFile, "rb");
		nRet = fread(puszMsgBuffer, 1, nSize, pSt_AFile);
		return 0;
	}
	return 0;
}

int Test_RTMPPush()
{
	XNETHANDLE xhStream = 0;
	LPCTSTR lpszUrl = _T("rtmp://192.168.4.37:1935/streamweb/qyt");
	BOOL bMemory = TRUE;

	if (bMemory)
	{
		pSt_VFile = _tfopen(lpszVFile, "rb");
		if (NULL == pSt_VFile)
		{
			printf("_tfopen:%d\n", GetLastError());
			return -1;
		}
		pSt_AFile = _tfopen(lpszAFile, "rb");
		if (NULL == pSt_VFile)
		{
			printf("_tfopen:%d\n", GetLastError());
			return -1;
		}

		if (!XClient_FilePush_Init(&xhStream))
		{
			printf("XClient_FilePush_Push:%lX\n", XClient_GetLastError());
			return -1;
		}
		if (!XClient_FilePush_Input(xhStream, NULL, NULL, fread_video, fread_audio, NULL, NULL))
		{
			printf("XClient_FilePush_Input:%lX\n", XClient_GetLastError());
			//return -1;
		}
		if (!XClient_FilePush_Output(xhStream, lpszUrl))
		{
			printf("XClient_FilePush_Output:%lX\n", XClient_GetLastError());
			return -1;
		}
		if (!XClient_FilePush_Start(xhStream))
		{
			printf("XClient_FilePush_Output:%lX\n", XClient_GetLastError());
			return -1;
		}
	}
	else
	{
		if (!XClient_FilePush_Init(&xhStream))
		{
			printf("XClient_FilePush_Push:%lX\n", XClient_GetLastError());
			return -1;
		}
		if (!XClient_FilePush_Input(xhStream, lpszUrl))
		{
			printf("XClient_FilePush_Input:%lX\n", XClient_GetLastError());
			return -1;
		}
		if (!XClient_FilePush_Output(xhStream, lpszUrl))
		{
			printf("XClient_FilePush_Output:%lX\n", XClient_GetLastError());
			return -1;
		}
		if (!XClient_FilePush_Start(xhStream))
		{
			printf("XClient_FilePush_Output:%lX\n", XClient_GetLastError());
			return -1;
		}
	}

	BOOL bIsPush = TRUE;
	while (bIsPush)
	{
		XClient_FilePush_GetStatus(xhStream, &bIsPush);
		Sleep(1000);
	}
	XClient_FilePush_Close(xhStream);
	return 1;
}

int CBStream_Pull(LPVOID lParam, uint8_t* puszMsgBuffer, int nSize)
{
	FILE* pSt_File = (FILE*)lParam;
	printf("%d\n", nSize);
	int nRet = fwrite(puszMsgBuffer, 1, nSize, pSt_File);

	return nRet;
}
int Test_RTMPPull()
{
	XNETHANDLE xhStream = 0;
	LPCTSTR lpszUrl = _T("rtmp://www.xyry.org:1935/live/qyt");
	LPCTSTR lpszFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\stream.flv");

	FILE* pSt_File = _tfopen(lpszFile, _T("wb"));
	if (NULL == pSt_File)
	{
		printf("file:%d\n", GetLastError());
		return -1;
	}

	if (!XClient_StreamPull_Init(&xhStream, lpszUrl, CBStream_Pull,pSt_File))
	{
		printf("XClient_FilePush_Push:%lX\n", XClient_GetLastError());
		return -1;
	}
	BOOL bPull = FALSE;
	XClient_StreamPull_GetStatus(xhStream, &bPull);

	while (bPull)
	{
		Sleep(1000);
	}
	XClient_StreamPull_Close(xhStream);

	return 1;
}

int main()
{
	WSADATA st_WSAData;
	WSAStartup(MAKEWORD(2, 2), &st_WSAData);
	Test_RTMPPush();
	//Test_RTMPPull();
	WSACleanup();
	return 0;
}